package com.gu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gu.VO.ServerResponse;
import com.gu.VO.UserVO;
import com.gu.entity.UserDO;
import com.gu.entity.VehicleRefuelingInfo;
import com.gu.entity.VehicleRefuelingRule;
import com.gu.enumeration.UserOccupationTypeEnum;
import com.gu.enumeration.UserStatusEnum;
import com.gu.mapper.VehicleRefuelingRuleMapper;
import com.gu.service.VehicleRefuelingRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gusongliao
 * @Description: 车辆加油规则实现类
 * @createDate 2023/01/08 13:09
 */
@Service
public class VehicleRefuelingRuleServiceImpl implements VehicleRefuelingRuleService {

    @Autowired
    private VehicleRefuelingRuleMapper ruleMapper;

    @Override
    public void checkRule(VehicleRefuelingInfo vehicleRefuelingInfo) {
        if (vehicleRefuelingInfo != null) {
            List<VehicleRefuelingRule> vehicleRules = ruleMapper.selectByStatus(1); // 1 启用， 0 未启动
            if (vehicleRules != null && vehicleRules.size() > 0) {
                VehicleRefuelingRule rule = vehicleRules.get(0);
                Integer drivingMileageMax = rule.getDrivingMileageMax(); // 加油里程表统计数最大值
                Integer drivingMileageMin = rule.getDrivingMileageMin(); // 加油里程表统计数最大值
                BigDecimal fuelLitersMax = rule.getFuelLitersMax(); // 燃油升数最大值
                BigDecimal fuelLitersMin = rule.getFuelLitersMin(); // 燃油升数最小值
                BigDecimal fuelUnitPriceMax = rule.getFuelUnitPriceMax(); // 燃油升数最大值
                BigDecimal fuelUnitPriceMin = rule.getFuelUnitPriceMin(); // 燃油升数最小值
                BigDecimal fuelTotalPriceMax = rule.getFuelTotalPriceMax(); // 燃油合计金额最大值
                BigDecimal fuelTotalPriceMin = rule.getFuelTotalPriceMin(); // 燃油合计金额最小值

                String vehicleName = vehicleRefuelingInfo.getVehiclename(); // 车牌号
                Integer drivingMileage = vehicleRefuelingInfo.getDrivingmileage();
                BigDecimal fuelLiters = vehicleRefuelingInfo.getFuelliters();
                BigDecimal fuelUnitPrice = vehicleRefuelingInfo.getFuelunitprice();
                BigDecimal fuelTotalPrice = vehicleRefuelingInfo.getFueltotalprice();
                if (drivingMileage == null || fuelLiters == null || fuelUnitPrice == null || fuelTotalPrice == null || vehicleName == null || "".equals(vehicleName)) {
                    vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                    return;
                }
                if (drivingMileageMax != null) {
                    if (drivingMileage > drivingMileageMax) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (drivingMileageMin != null) {
                    if (drivingMileage < drivingMileageMin) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelLitersMax != null) {
                    if (fuelLiters.compareTo(fuelLitersMax) > 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelLitersMin != null) {
                    if (fuelLiters.compareTo(fuelLitersMin) < 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelUnitPriceMax != null) {
                    if (fuelUnitPrice.compareTo(fuelUnitPriceMax) > 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelUnitPriceMin != null) {
                    if (fuelUnitPrice.compareTo(fuelUnitPriceMin) < 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelTotalPriceMax != null) {
                    if (fuelTotalPrice.compareTo(fuelTotalPriceMax) > 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
                if (fuelTotalPriceMin != null) {
                    if (fuelTotalPrice.compareTo(fuelTotalPriceMin) < 0) {
                        vehicleRefuelingInfo.setSuspectedErrorFlag(1);
                        return;
                    }
                }
            }
        }
    }

    @Override
    public PageInfo ruleList(Integer pageNum, Integer pageSize, Integer status){
        PageHelper.startPage(pageNum,pageSize);
        List<VehicleRefuelingRule> vehicleRules = ruleMapper.selectByStatus(1); // 1 启用， 0 未启动
        PageInfo pageInfo = PageInfo.of(vehicleRules);
        return pageInfo;
    }

    @Override
    public ServerResponse updateRule(VehicleRefuelingRule vehicleRefuelingRule) {
        int i = ruleMapper.updateByPrimaryKey(vehicleRefuelingRule);
        if (i == 0) {
            return ServerResponse.error(500, "更新失败");
        } else {
            return ServerResponse.SUCCESS;
        }
    }
}
